Windows 物联网 Raspberry Pi 3 C# RTC DS3231
Windows IoT Raspberry Pi 3 C# RTC DS3231
这个 post 可能看起来与 Windows 物联网和 DS3231 RTC 时钟重复
但它似乎对我不起作用。我没有在这种环境中使用 i2c 的经验。
我的预期功能是,
- 在启动时检查是否有任何网络连接来更新系统时钟
- 如果离线,系统将从
DS3231
读取 datetime
并更新
系统 datetime
datetime
与 datepicker
的任何变化或
timepicker
它将更新 DS3231
我的问题是
- 如何检查网络可用性以更新系统时钟。
阅读 DS3231
使用 i1cdevice.writeread
需要分别 write
read address
吗?
i2c
是否会自动读取所有数据,直到收到 stop
位,或者我必须设置一个计数器?
private const byte DS3231_I2C_WADDR = 0xD0;
private const byte DS3231_I2C_RADDR = 0xD1;
private I2cDevice DS3231_RTC;
byte[] i2CWriteBuffer;
byte[] i2CReadBuffer;
private async void Read_DS3231_RTC()
{
var settings = new I2cConnectionSettings(DS3231_I2C_RADDR);
settings.BusSpeed = I2cBusSpeed.StandardMode;
var controller = await I2cController.GetDefaultAsync();
DS3231_RTC = controller.GetDevice(settings);
try
{
DS3231_RTC.WriteRead(new byte[] { DS3231_I2C_RADDR }, i2CReadBuffer);
}
catch (Exception e)
{
StatusMessage.Text = "Fail to Init I2C:" + e.Message;
return;
}
}
写入DS3231
时间设置有dateicker
& timepicker
设置 datetime
后,我如何更新为 DS3231
private void DatePicker_DateChanged(object sender, DatePickerValueChangedEventArgs e)
{
DateTimeSettings.SetSystemDateTime(e.NewDate.UtcDateTime);
SetTime_DS3231();
}
private void TimePicker_TimeChanged(object sender, TimePickerValueChangedEventArgs e)
{
var currentDate = DateTime.Now.ToUniversalTime();
var newDateTime = new DateTime(currentDate.Year,
currentDate.Month,
currentDate.Day,
e.NewTime.Hours,
e.NewTime.Minutes,
e.NewTime.Seconds);
DateTimeSettings.SetSystemDateTime(newDateTime);
}
谢谢。
1.How to check for network availibility to update system clock.
您可以使用 NetworkInterface class 的 GetIsNetworkAvailable 方法来检查是否已连接互联网。您可以从此 主题中获得有关如何在通用 Windows 平台中检查互联网连接类型的更多知识
.
bool isInternetConnected = NetworkInterface.GetIsNetworkAvailable();
2.to read from DS3231 using i1cdevice.writeread requires to write read address separately? does the i2c automatically reads all the data
until stop bit is received or i have to setup a counter ?
没必要分开写读。 WriteRead 方法执行原子操作,将数据写入设备所连接的 inter-integrated 电路 (I2 C) 总线,然后从中读取数据,并在写入和读取操作之间发送重启条件。第二个参数是要从 I2 C 总线读取数据的缓冲区。缓冲区的长度决定了从 device.It 请求多少数据不会自动停止,直到停止位为 received.If I2 C 设备在整个缓冲区被读取之前否定确认数据传输,将会有错误(错误代码 0x8007045D)。
3.to write to DS3231 there are dateicker & timepicker for time setting upon setting the datetime how could I update to DS3231
如TRS在话题Windows中回复的物联网和DS3231 RTC时钟
,he/she提供设置时钟的方法
新更新:
private async void SetTime_DS3231(DateTime dt)
{
int SlaveAddress = 0x68;
try
{
// Initialize I2C
var Settings = new I2cConnectionSettings(SlaveAddress);
Settings.BusSpeed = I2cBusSpeed.StandardMode;
if (AQS == null || DIS == null)
{
AQS = I2cDevice.GetDeviceSelector("I2C1");
DIS = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(AQS);
}
using (I2cDevice Device = await I2cDevice.FromIdAsync(DIS[0].Id, Settings))
{
byte write_seconds = decToBcd((byte)dt.Second);
byte write_minutes = decToBcd((byte)dt.Minute);
byte write_hours = decToBcd((byte)dt.Hour);
byte write_dayofweek = decToBcd((byte)dt.DayOfWeek);
byte write_day = decToBcd((byte)dt.Day);
byte write_month = decToBcd((byte)dt.Month);
byte write_year = decToBcd((byte)(dt.Year%100));
byte[] write_time = { 0x00, write_seconds, write_minutes, write_hours, write_dayofweek, write_day, write_month, write_year };
Device.Write(write_time);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex.Message);
}
}
private static byte decToBcd(byte val)
{
return (byte)(((int)val / 10 * 16) + ((int)val % 10));
}
这个 post 可能看起来与 Windows 物联网和 DS3231 RTC 时钟重复 但它似乎对我不起作用。我没有在这种环境中使用 i2c 的经验。
我的预期功能是,
- 在启动时检查是否有任何网络连接来更新系统时钟
- 如果离线,系统将从
DS3231
读取datetime
并更新 系统datetime
datetime
与datepicker
的任何变化或timepicker
它将更新DS3231
我的问题是
- 如何检查网络可用性以更新系统时钟。
阅读
DS3231
使用i1cdevice.writeread
需要分别write
read address
吗?i2c
是否会自动读取所有数据,直到收到stop
位,或者我必须设置一个计数器?private const byte DS3231_I2C_WADDR = 0xD0; private const byte DS3231_I2C_RADDR = 0xD1; private I2cDevice DS3231_RTC; byte[] i2CWriteBuffer; byte[] i2CReadBuffer; private async void Read_DS3231_RTC() { var settings = new I2cConnectionSettings(DS3231_I2C_RADDR); settings.BusSpeed = I2cBusSpeed.StandardMode; var controller = await I2cController.GetDefaultAsync(); DS3231_RTC = controller.GetDevice(settings); try { DS3231_RTC.WriteRead(new byte[] { DS3231_I2C_RADDR }, i2CReadBuffer); } catch (Exception e) { StatusMessage.Text = "Fail to Init I2C:" + e.Message; return; } }
写入
DS3231
时间设置有dateicker
&timepicker
设置datetime
后,我如何更新为DS3231
private void DatePicker_DateChanged(object sender, DatePickerValueChangedEventArgs e) { DateTimeSettings.SetSystemDateTime(e.NewDate.UtcDateTime); SetTime_DS3231(); } private void TimePicker_TimeChanged(object sender, TimePickerValueChangedEventArgs e) { var currentDate = DateTime.Now.ToUniversalTime(); var newDateTime = new DateTime(currentDate.Year, currentDate.Month, currentDate.Day, e.NewTime.Hours, e.NewTime.Minutes, e.NewTime.Seconds); DateTimeSettings.SetSystemDateTime(newDateTime); }
谢谢。
1.How to check for network availibility to update system clock.
您可以使用 NetworkInterface class 的 GetIsNetworkAvailable 方法来检查是否已连接互联网。您可以从此 主题中获得有关如何在通用 Windows 平台中检查互联网连接类型的更多知识 .
bool isInternetConnected = NetworkInterface.GetIsNetworkAvailable();
2.to read from DS3231 using i1cdevice.writeread requires to write read address separately? does the i2c automatically reads all the data until stop bit is received or i have to setup a counter ?
没必要分开写读。 WriteRead 方法执行原子操作,将数据写入设备所连接的 inter-integrated 电路 (I2 C) 总线,然后从中读取数据,并在写入和读取操作之间发送重启条件。第二个参数是要从 I2 C 总线读取数据的缓冲区。缓冲区的长度决定了从 device.It 请求多少数据不会自动停止,直到停止位为 received.If I2 C 设备在整个缓冲区被读取之前否定确认数据传输,将会有错误(错误代码 0x8007045D)。
3.to write to DS3231 there are dateicker & timepicker for time setting upon setting the datetime how could I update to DS3231
如TRS在话题Windows中回复的物联网和DS3231 RTC时钟 ,he/she提供设置时钟的方法
新更新:
private async void SetTime_DS3231(DateTime dt)
{
int SlaveAddress = 0x68;
try
{
// Initialize I2C
var Settings = new I2cConnectionSettings(SlaveAddress);
Settings.BusSpeed = I2cBusSpeed.StandardMode;
if (AQS == null || DIS == null)
{
AQS = I2cDevice.GetDeviceSelector("I2C1");
DIS = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(AQS);
}
using (I2cDevice Device = await I2cDevice.FromIdAsync(DIS[0].Id, Settings))
{
byte write_seconds = decToBcd((byte)dt.Second);
byte write_minutes = decToBcd((byte)dt.Minute);
byte write_hours = decToBcd((byte)dt.Hour);
byte write_dayofweek = decToBcd((byte)dt.DayOfWeek);
byte write_day = decToBcd((byte)dt.Day);
byte write_month = decToBcd((byte)dt.Month);
byte write_year = decToBcd((byte)(dt.Year%100));
byte[] write_time = { 0x00, write_seconds, write_minutes, write_hours, write_dayofweek, write_day, write_month, write_year };
Device.Write(write_time);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex.Message);
}
}
private static byte decToBcd(byte val)
{
return (byte)(((int)val / 10 * 16) + ((int)val % 10));
}